<?php
/**
 * 管理员列表服务
 * User: dxk
 * Date: 2020/07/20
 */

namespace App\Service\Core\Admin\User;

use App\Component\ListService\ListAbstract;
use App\Model\Core\Admin\AdminUserModel;
use App\Model\Core\Admin\AdminUserRoleModel;
use App\Service\ParamsService;

/**
 * @method $this setRoleId($val) role_id
 * @method $this setUserIds($val)
 * @package App\Service\Core\Sys\User
 */
class AdminUserListService extends ListAbstract
{
    //允许获取的字段
    protected array $allow_fields = ['*'];
    //允许获取的扩展字段
    protected array $allow_ext_fields = ['status_name', 'role_name', 'allow_edit'];
    protected array $allow_search_fields = ['username', 'mobile'];
    //默认返回的字段
    protected array $default_fields = [];
    protected array $allow_sort = ['login_at', 'created_at', 'user_id'];
    protected array $default_sort_by = ['user_id' => 'desc'];

    public function setUsername($val)
    {
        $this->params['username'] = trim($val);
        return $this;
    }

    protected function getQueryObj()
    {
        // TODO: Implement getQueryObj() method.
        $UserModel = AdminUserModel::getInstance();
        $query_obj = $UserModel->getConnectionTable();
        //用户名称
        $username = $this->getParam('username');
        if (!empty($username)) {
            $query_obj->where('username', 'like', $username);
        }
        //用户类型
        $role_id = (int)$this->getParam('role_id');
        if (!empty($role_id)) {
            $query_obj->whereRaw('find_in_set(' . $role_id . ',role_id)');
        }
        //用户ID
        $user_ids = $this->getParam('user_ids');
        $user_ids = easy_int_explode($user_ids);
        if (!empty($user_ids)) {
            $query_obj->whereIn('user_id', $user_ids);
        }
        //绑定手机号
        $mobile = $this->getParam('mobile');
        if (!empty($mobile)) {
            $query_obj->where('mobile', $mobile);
        }
        //状态
        $status = $this->getParam('status');
        if (is_numeric($status)) {
            $query_obj->where('status', $status);
        }
        //开始时间
        $created_at_begin = $this->getParam('created_at_begin');
        if (!empty($created_at_begin)) {
            $created_at_begin = date('Y-m-d H:i:s', strtotime($created_at_begin));
            $query_obj->where('created_at', '>=', $created_at_begin);
        }
        //结束时间
        $created_at_end = $this->getParam('created_at_end');
        if (!empty($created_at_end)) {
            $created_at_end = date('Y-m-d H:i:s', strtotime($created_at_end));
            $query_obj->where('created_at', '<=', $created_at_end);
        }
        return $query_obj;
    }

    /**
     * 获取扩展字段
     * @param array $row
     * @return array
     */
    protected function getExtField(array $row)
    {
        if (easy_in_array('status_name', $this->ext_fields)) {
            $row['status_name'] = ParamsService::$status[$row['status']];
        }
        if (easy_in_array('role_name', $this->ext_fields)) {
            $role_ids = $row['role_id'] ? explode(',', $row['role_id']) : [];
            $role_names = [];
            $row['allow_edit'] = 1;
            foreach ($role_ids as $role_id) {
                $role = AdminUserRoleModel::getInstance()->getOne($role_id);
                $role_names[] = $role['name'];
            }
            $row['role_name'] = implode(',', $role_names);
        }
        return $row;
    }
}
